package com.focusai.efairy.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.focusai.efairy.R;

/**
 * Created by luo_guangwen on 2017/10/16.
 */

public class SearchTittleView extends LinearLayout implements View.OnClickListener {
    private ImageView searchClearIcon;
    private EditText txtSearchedit;
    private TextView textInputHint;
    private OnSearchListener searchListener;
    private LinearLayout ll_enter_input;
    private RelativeLayout rl_search;
    public SearchTittleView(Context context) {
        super(context);
        initView(context,null);
    }

    public SearchTittleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    public SearchTittleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    private void initView(Context context,AttributeSet attrs) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.view_search, this);
        rootView.findViewById(R.id.text_cancal_searchBtn).setOnClickListener(this);
        rootView.findViewById(R.id.ll_enter_input).setOnClickListener(this);
        rootView.findViewById(R.id.search_header_clear).setOnClickListener(this);
        searchClearIcon = (ImageView) rootView.findViewById(R.id.search_header_clear);
        txtSearchedit = (EditText) rootView.findViewById(R.id.txtSearchedit);
        textInputHint = (TextView) rootView.findViewById(R.id.text_search_enter_hint);
        ll_enter_input = (LinearLayout) rootView.findViewById(R.id.ll_enter_input);
        rl_search = (RelativeLayout) rootView.findViewById(R.id.rl_search);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.search_view);
        if (array != null){
            CharSequence inputhint = array.getString(R.styleable.search_view_input_text_hint);
            CharSequence searchhint = array.getString(R.styleable.search_view_search_edit_hint);
            txtSearchedit.setHint(searchhint);
            textInputHint.setText(inputhint);
            array.recycle();
        }
        bindEvent();
    }

    private void bindEvent() {
        txtSearchedit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String keyword = txtSearchedit.getText().toString();
                if (keyword == null || keyword.length() <= 0) {
                    searchClearIcon.setVisibility(View.GONE);
                } else {
                    searchClearIcon.setVisibility(View.VISIBLE);
                    if (searchListener != null) {
                        searchListener.onStartQuery(txtSearchedit.getText().toString(),false);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        txtSearchedit.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (searchListener != null) {
                    String keywork = txtSearchedit.getText().toString();
                    if (!TextUtils.isEmpty(keywork)) {
                        searchListener.onStartQuery(keywork,true);
                    }
                }
                return true;
            }
        });
    }

    public void setSearchListener(OnSearchListener searchListener) {
        this.searchListener = searchListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_header_clear:
                txtSearchedit.setText("");
                if (searchListener != null) {
                    searchListener.onStartQuery("",false);
                }
                break;
            case R.id.text_cancal_searchBtn:
                ll_enter_input.setVisibility(VISIBLE);
                rl_search.setVisibility(GONE);
                if (searchListener != null){
                    searchListener.onCancalBackClick();
                }
                break;
            case R.id.ll_enter_input:
                txtSearchedit.setText("");
                ll_enter_input.setVisibility(GONE);
                rl_search.setVisibility(VISIBLE);
                txtSearchedit.setFocusable(true);
                txtSearchedit.setFocusableInTouchMode(true);
                txtSearchedit.requestFocus();
                //打开软键盘
                InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//                setFocusable(true);
//                setFocusableInTouchMode(true);
//                requestFocus();
                if (searchListener != null){
                    searchListener.onEnterInputKeyWork();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (rl_search.getVisibility() == VISIBLE){
                ll_enter_input.setVisibility(VISIBLE);
                rl_search.setVisibility(GONE);
                if (searchListener != null){
                    searchListener.onCancalBackClick();
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public interface OnSearchListener {
        void onStartQuery(String keywork,boolean hideInput);

        void onCancalBackClick();

        void onEnterInputKeyWork();
    }
}
